package com.firewolf.fr.s001_helloworld;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class Freemaker01Hello {

	public static void main(String[] s ) throws IOException, TemplateException{
		
		Template t = new Template("hello", new StringReader("hello, ${name}！\n\t"));
		
		Map root = new HashMap();
		root.put("name", "freemarker");
		
		t.process(root, new OutputStreamWriter(System.out));
		
//		useFtl();
		
//		useCfg() ;
		
	}
	/**
	 * 直接使用Template
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @throws TemplateException
	 */
	public static void useFtl() throws FileNotFoundException, IOException, TemplateException{
		File f = new File("E:\\workspace_eclipse_luna\\freemarker01\\src\\main\\java\\com\\bw\\mb.ftl");
		System.out.println(f.getPath());
		
		Template t = new Template("mb", new FileReader(f));
		
		Map m = new HashMap<String, Object>();
		
		m.put("user", "freemark");
		m.put("url", "www.baidu.com");
		m.put("name", "百度");
		
		t.process(m, new OutputStreamWriter(System.out));
		
	}
	/**
	 * 使用cfg
	 * @throws IOException
	 * @throws TemplateException
	 */
	public static void useCfg() throws IOException, TemplateException{
		
		Configuration cfg = new Configuration();
		
		cfg.setDirectoryForTemplateLoading(new File("E:\\workspace_eclipse_luna\\freemarker01\\src\\main\\java\\com\\bw"));
		
		Template t = cfg.getTemplate("mb2.ftl");

		Map m = new HashMap<String, Object>();
		
		m.put("user", "mb2");
		m.put("url", "www.baidu.com");
		m.put("name", "百度");
		m.put("currentDate", new Date());

		t.process(m, new OutputStreamWriter(System.out));
		
	}
	
	

}
